package com.example.fourtestapp.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.fourtestapp.R;
import com.example.fourtestapp.dbhelper.DBhelper;
import com.example.fourtestapp.entity.BillInfo;
import com.example.fourtestapp.entity.CaridInfo;
import com.example.fourtestapp.entity.MessgInfo;
import com.example.fourtestapp.utils.HttpUtils;
import com.example.fourtestapp.utils.ParamUtlis;
import com.example.fourtestapp.utils.RegUtils;
import com.google.gson.Gson;
import com.j256.ormlite.dao.Dao;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MyAccFragment extends Fragment {

    private View view;
    private TextView yuE;
    private Spinner sp;
    private EditText je;
    private Button chaxun;
    private Button chongzhi;
    private List<String> carids = new ArrayList<>();
    private Dao<BillInfo, Integer> billInfoDao;
    private BillInfo billInfos = new BillInfo();
    private CaridInfo caridInfo = new CaridInfo();
    private MessgInfo messgInfo = new MessgInfo();
    private SimpleDateFormat sf = new SimpleDateFormat("MM-dd HH:mm:ss");
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 100:
                    Toast.makeText(getContext(), "充值成功", Toast.LENGTH_SHORT).show();
                    yuE.setText(messgInfo.getBalance() + "元");
                    break;
                case 200:
                    yuE.setText(messgInfo.getBalance() + "元");
                    break;
                case 400:
                    for (int i = 0; i < caridInfo.getROWS_DETAIL().size(); i++) {
                        carids.add(caridInfo.getROWS_DETAIL().get(i).getCarId());
                    }
                    ArrayAdapter arrayAdapter = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1, carids);
                    sp.setAdapter(arrayAdapter);
                    sp.setSelection(0);
                    break;
                case 600:
                    yuE.setText(msg.obj.toString() + "元");
                    break;
            }
        }
    };

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_my_acc, container, false);
        carids.clear();
        billInfoDao = null;
        initView(view);
        initdata();
        GetData();
        initListeren();
        return view;
    }


    private void initView(View view) {
        yuE = view.findViewById(R.id.yu_e);
        sp = view.findViewById(R.id.sp);
        je = view.findViewById(R.id.je);
        chaxun = view.findViewById(R.id.chaxun);
        chongzhi = view.findViewById(R.id.chongzhi);
        try {
            billInfoDao = new DBhelper(getContext()).getbilDao();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initListeren() {
//        chaxun.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                String data = new ParamUtlis()
//                        .setParam("CarId", sp.getSelectedItem().toString())
//                        .setParam("UserName", "user1")
//                        .build();
//                HttpUtils.Post("/GetCarAccountBalance", data, new Callback() {
//                    @Override
//                    public void onFailure(Call call, IOException e) {
//                        e.printStackTrace();
//                    }
//                    @Override
//                    public void onResponse(Call call, Response response) throws IOException {
//                        String data = response.body().string();
//                        messgInfo = new Gson().fromJson(data, MessgInfo.class);
//                        handler.sendEmptyMessage(200);
//                    }
//                });
//
//            }
//        });


        sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String data = new ParamUtlis()
                        .setParam("CarId", sp.getSelectedItem().toString())
                        .setParam("UserName", "user1")
                        .build();
                HttpUtils.Post("/GetCarAccountBalance", data, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String data = response.body().string();
                        messgInfo = new Gson().fromJson(data, MessgInfo.class);
                        handler.sendEmptyMessage(200);
                    }
                });
            }

            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });

        chongzhi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String jee = je.getText().toString();
                int czje = Integer.parseInt(je.getText().toString());
                if (RegUtils.Check(jee, 0, 1000) && czje != 0) {
                    String data = new ParamUtlis()
                            .setParam("CarId", sp.getSelectedItem().toString())
                            .setParam("Money", je.getText().toString())
                            .setParam("UserName", "user1")
                            .build();
                    HttpUtils.Post("/SetCarAccountRecharge", data, new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            e.printStackTrace();
                        }

                        public void onResponse(Call call, Response response) throws IOException {
                            String data = response.body().string();
                            messgInfo = new Gson().fromJson(data, MessgInfo.class);
                            billInfos.setCarid(sp.getSelectedItem().toString());
                            billInfos.setJe(je.getText().toString());
                            billInfos.setUser("user1");
                            billInfos.setTime(sf.format(new Date()));
                            try {
                                billInfoDao.create(billInfos);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            handler.sendEmptyMessage(100);
                        }
                    });
                } else {
                    Toast.makeText(getContext(), "输入错误", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void GetData() {
        String data = new ParamUtlis().setParam("UserName", "user1").build();
        HttpUtils.Post("/GetCarInfo", data, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String data = response.body().string();
                caridInfo = new Gson().fromJson(data, CaridInfo.class);
                handler.sendEmptyMessage(400);
            }
        });


    }

    private void initdata() {
        String data = new ParamUtlis()
                .setParam("CarId", "1")
                .setParam("UserName", "user1")
                .build();
        HttpUtils.Post("/GetCarAccountBalance", data, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String data = response.body().string();
                messgInfo = new Gson().fromJson(data, MessgInfo.class);
                Message message = new Message();
                message.what = 600;
                message.obj = messgInfo.getBalance();
                handler.sendMessage(message);
            }
        });
    }
}
